// https://leetcode.cn/problems/4sum/submissions/

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> res;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); ++i) {
            if (i && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < nums.size(); ++j) {
                // if (nums[j] == nums[j - 1]) continue;
                if (j != i + 1 && nums[j] == nums[j - 1]) continue;
                long tmp = (long)target - nums[i] - nums[j];
                int low = j + 1;
                int high = nums.size() - 1;
                while (low < high) {
                    if (nums[low] + nums[high] == tmp) {
                        vector<int> tmpRes = {nums[i], nums[j], nums[low], nums[high]};
                        res.push_back(tmpRes);
                        low++;
                        high--;
                        while (low < high && nums[low] == nums[low - 1]) low++;
                        while (low < high && nums[high] == nums[high + 1]) high--;
                    } else if (nums[low] + nums[high]< tmp) low++;
                    else high--;
                }
            }
        }
        return res;
    }
};